/*
Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method: 

1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

2. If the length of the sub-string is 1, '1' should be ignored.
 

Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
 

Output
For each test case, output the encoded string in a line.
 

Sample Input
2
ABC
ABBCCC
 

Sample Output
ABC
A2B3C
 */
package com.yuan.algorithms.training201607;

import java.io.BufferedInputStream;
import java.util.Scanner;

/**
 * @author YouYuan
 * @contact 1265161633@qq.com
 * @date 2016年8月5日 上午8:44:23
 * @descript 
 */
public class HDU1020字符串编码 {

	static Scanner in = new Scanner(new BufferedInputStream(System.in));
	public static void main(String[] args) {
		while(in.hasNext()) {
			int n = in.nextInt();
			for (int i = 0; i < n; i++) {
				String str = in.next();
				String encodingStr = encoding(str);
				System.out.println(encodingStr);
			}
		}
	}
	private static String encoding(String str) {
		StringBuilder encode = new StringBuilder();
		for (int i = 0, len = str.length(); i < len; ++i) {
			char c = str.charAt(i);
			int count = 1;
			while(i + 1 < len) {
				char temp = str.charAt(++i);
				if (temp == c) {
					++count;
				} else {
					--i;
					break;
				}
			}
			if (count != 1) {
				encode.append(count);
			}
			encode.append(c);
		}
		return encode.toString();
	}

}
